<?php

function numeroMeses($fim, $fia, $ffm, $ffa) {
    $meses = 0;
    if ($fia == $ffa) {
        for ($i = $fim; $i <= $ffm; $i++) {
            $meses++;
        }
    } else {
        //////////
        if ($fia < $ffa) {
            $anio = 0;
            $anio = $ffa - $fia;
            if ($anio == 1) {
                for ($i = $fim; $i <= 12; $i++) {
                    $meses++;
                }

                for ($i = 1; $i <= $ffm; $i++) {
                    $meses++;
                }
            } else if ($anio > 1) {
                for ($i = $fim; $i <= 12; $i++) {
                    $meses++;
                }

                for ($i = $fia + 1; $i <= $ffa - 1; $i++) {
                    for ($j = 1; $j <= 12; $j++) {
                        $meses++;
                    }
                }
                for ($i = 1; $i <= $ffm; $i++) {
                    $meses++;
                }
            }
        }
    }
    return $meses;
}

$fi = explode("-", $proyecto->getPeriodos()->getFechai());
$ff = explode("-", $proyecto->getPeriodos()->getFechaf());
$ainicial = $fi[0];
$afinal = $ff[0];
$minicial = $fi[1];
$mfinal = $ff[1];
$auxval = 0;
?>
<div id="titulosproyecto" align="center"><h1>CRONOGRAMA <b>ENTIDAD <?php echo strtoupper($fondo->getSiglasInst()) ?></b></h1></div><br>
<div id="cajaprincipalproyecto" align="center">

    <div id="contextual" class="contenedor">
        <div align="left"><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSECRETARÍA NACIONAL DE EDUCACIÓN SUPERIOR, CIENCIA, TECNOLOGÍA E INNOVACIÓN</br></div>
        <div align="left"><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSUBSECRETARÍA DE INVESTIGACIÓN CIENTÍFICA<br></div>
        <div align="left"><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspDIRECCIÓN DE MONITOREO, SEGUIMIENTO Y EVALUACIÓN DE PROGRAMAS Y PROYECTOS DE INVESTIGACIÓN CIENTÍFICA</br></div>
        <div align="left"><br><b>Código:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<?php echo $proyecto->getCodigo() ?></b></br></div>
        <div align="left"><br><b>Proyecto:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<?php echo $proyecto->getNombre() ?></b></br></div>
        <div align="left"><br><b>Director:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<?php echo $proyecto->getDirector() ?></b></br></div>
        <div align="left"><br><b>Institución:&nbsp&nbsp<?php echo $fondo->getNInst() ?></b></br></div><br><br>
        <table>
            <tr>
                <td style="background-color: red;"><B>CRONOGRAMA VALORADO DE EJECUCIÓN POR OBJETIVOS, ENTIDAD <?php echo strtoupper($fondo->getSiglasInst()) ?></B></td>
            </tr>
        </table><br>
        <div id="fondos" align="center">
            <table border="1">
                <thead>
                    <?php include_partial('tdaniocronograma') ?>
                </thead>
                <?php
                $c1 = 0;
                $meses = 0;
                $mesespro = numeroMeses($minicial, $ainicial, $mfinal, $afinal);
                foreach ($objetivos as $ob):
                    $fio = explode("-", $ob->getPeriodos()->getFechai());
                    $ffo = explode("-", $ob->getPeriodos()->getFechaf());
                    $ainicialo = $fio[0];
                    $afinalo = $ffo[0];
                    $minicialo = $fio[1];
                    $mfinalo = $ffo[1];
                    $c1++;
                    $n = numeroMeses($minicialo, $ainicialo, $mfinalo, $afinalo);
                    ?>
                    <tr>
                        <td colspan="9" style="background-color: thistle"><b>Objetivo Específico <?php echo $c1 ?>:<br><?php echo $ob->getNombre() ?></b></td>                   
                    </tr>
                    <?php
                    $listacti = Doctrine_Core::getTable('actividad')->createQuery('a')
                            ->where('a.id_ob = ?', $ob->getIdOb())
                            ->execute();
                    $c2 = 0;
                    foreach ($listacti as $ac):
                        $lvac = Doctrine_Core::getTable('valor')->createQuery('v')
                                ->andwhere('v.id_ac = ?', $ac->getIdAc())
                                ->andwhere('v.id_fo = ?', $fondo->getIdFo())
                                ->execute();

                        $ban = false;
                        $c2++;
                        ?>
                        <tr>
                            <td style="background-color: skyblue" colspan="9"><b>Actividad <?php echo $c1 ?>.<?php echo $c2 ?>:<br><?php echo $ac->getNombre() ?></b></td> 
                        </tr>                     
                        <?php
                        $c3 = 0;
                        foreach ($techos as $te):
                            $c3++;
                            ?>
                            <tr>
                                <td style="background-color: #f33" colspan="9">&nbsp;<b><?php echo $c3 ?>&nbsp;<?php echo $te->getNombre() ?></b></td>                        
                            </tr>                     
                            <?php
                            foreach ($descripciones as $des):
                                if ($des->getIdTp() == $te->getIdTp()) {
                                    $listva = Doctrine_Core::getTable('valor')->createQuery('v')
                                            ->where('v.id_dg = ?', $des->getIdDg())
                                            ->andwhere('v.id_ac = ?', $ac->getIdAc())
                                            ->andwhere('v.id_fo = ?', $fondo->getIdFo())
                                            ->execute();
                                    ?>
                                    <tr>
                                        <td colspan="6"><?php echo $des->getNombre() ?></td>
                                        <td colspan="3" align="center"><div id="cajacodigo"><?php echo $des->getCodigo() ?></div></td>
                                        <?php
                                        if ($c1 == 1) {
                                            foreach ($listva as $vas):
                                                ?>
                                                <td valign="baseline" align="center"><?php echo $vas->getVal() ?></td>
                                                <?php
                                            endforeach;
                                            for ($i = $n; $i < $mesespro; $i++) {
                                                ?>
                                                <td valign="baseline" align="center">00.00</td>
                                                <?php
                                            }
                                        } else {
                                            for ($i = 1; $i <= $auxval; $i++) {
                                                ?>
                                                <td valign="baseline" align="center">00.00</td>
                                                <?php
                                            }
                                            foreach ($listva as $vas):
                                                ?>
                                                <td valign="baseline" align="center"><?php echo $vas->getVal() ?></td>
                                                <?php
                                            endforeach;
                                            if ($auxval == $mesespro) {
                                                
                                            } else {
                                                for ($i = $auxval + count($listva); $i < $mesespro; $i++) {
                                                    ?>
                                                    <td valign="baseline" align="center">00.00</td>
                                                    <?php
                                                }
                                            }
                                        }

                                        $fils = 0;
                                        foreach ($listva as $vas):
                                            $fils = $fils + $vas->getVal();
                                        endforeach;
                                        ?>

                                        <td valign="middle" align="center"><?php echo $fils ?></td>
                                        <?php
                                        if ($ban == false) {
                                            $sac = 0;
                                            foreach ($lvac as $vac):
                                                $sac = $sac + $vac->getVal();
                                            endforeach;
                                            ?>
                                            <td style="background-color: skyblue"  rowspan="<?php echo count($techos) + count($descripciones) - 1 ?>"><?php echo $sac ?></td>
                                            <?php
                                            $ban = true;
                                        }
                                        ?>

                                    </tr>
                                    <?php
                                }
                            endforeach;


                        endforeach;
                    endforeach;
                    ?>  
                    <tr style="background-color: #328a26;">
                        <td align="center" colspan="9">SUBTOTAL DEL OBJETIVO <?php echo $c1 ?>:</td>
                        <?php include_partial('tablefootercronograma', array('id_fo' => $fondo->getIdFo(), 'id_ob' => $ob->getIdOb(), 'id_pro' => $proyecto->getIdPro())) ?>
                    </tr>
                    <?php
                    $auxval = $auxval + $n;
                endforeach;
                ?>


                <tr >
                    <td><?php include_partial('tablefooter2cronograma') ?></td>
                </tr>


                <?php
//include_partial('tablefooter2cronograma');
                ?>


            </table>
            <br><br>
        </div>

    </div><br>    
    <?php $this->user = sfContext::getInstance()->getUser()->setAttribute('id_foaux', $fondo->getIdFo()); ?>
    <div align="center">
        <a href="<?php echo url_for('Reportes/CronogramaFondo2?id_fo=' . $fondo->getIdFo()) ?>"><button class="buttonmediumauto button-green"><span><div id="imagenes" valign="middle"><div  align="center" class="spritesexportresmall" id="exportresmall_jpg" align="center"></div></div><div id="textobutton2">Exportar a Excel</div></span></button></a>
        <a href="<?php echo url_for('Reportes/CronogramaFondo2L?id_fo=' . $fondo->getIdFo()) ?>"><button class="buttonmediumauto button-green"><span><div id="imagenes" valign="middle"><div  align="center" class="spritesexportresmall" id="exportresmall_jpg" align="center"></div></div><div id="textobutton2">Exportar a Open Calc</div></span></button></a>
<!--            <a href="<?php echo url_for('Reportes/CronogramaFondoPDF?id_fo=' . $fondo->getIdFo()) ?>"><button class="buttonmediumauto button-green"><span><div id="imagenes" valign="middle"><div  align="center" class="spritesexportresmall" id="exportresmall_jpg" align="center"></div></div><div id="textobutton2">Exportar a PDF</div></span></button></a>-->
        <?php
        $fondosaux = Doctrine_Core::getTable('fondo')->createQuery('f')
                ->where('f.id_pro = ?', $pro)
                ->execute();
        if (count($fondosaux) == 2) {
            ?>
            <a href="<?php echo url_for('Reportes/Cronogramas?id_pro=' . $pro) ?>"><button class="buttonmediumauto button-green"><span><div id="imagenes" valign="center"><div align="center"  class="spritesatrasmall" id="atrasmall_jpg" align="center"></div></div><div id="textobutton2">Regresar a Cronogramas</div></span></button></a><br><br>
        <?php } else { ?>
            <a href="<?php echo url_for('Reportes/index?id_pro=' . $proyecto->getIdPro()) ?>"><button class="buttonmediumauto button-green"><span><div id="imagenes" valign="center"><div align="center"  class="spritesatrasmall" id="atrasmall_jpg" align="center"></div></div><div id="textobutton2">Regresar </div></span></button></a><br><br>
        <?php } ?>



    </div><br><br>

</div>